package com.haierp.controller.bi;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haierp.mapperBi.BiSaleSkuMonthMapper;
import com.haierp.model.bi.BiPurchaseStorageLa;
import com.haierp.model.bi.BiReturnReason;
import com.haierp.model.bi.BiSale;
import com.haierp.model.bi.BiSaleSizeQuantity;
import com.haierp.model.bi.BiSaleSkuMonth;
import com.haierp.model.bi.BiSalesVolumeDay;
import com.haierp.service.bi.IBiSaleService;
import com.haierp.util.NumberUtil;
import com.haierp.vo.json.PicModel;
import com.wangzhixuan.commons.base.BaseController;
import com.wangzhixuan.commons.shiro.ShiroUser;
import com.wangzhixuan.commons.utils.HaiJsonUtils;

@Controller
@RequestMapping("/bi/sale")
public class BiSaleController extends BaseController {
	
	@Autowired
	private IBiSaleService biSaleService;
	@Autowired
	private BiSaleSkuMonthMapper biSaleSkuMonthMapper;
	
	@RequestMapping("/selectSaleReportByGmtCreate")
	public String selectSaleReportByGmtCreate(HttpServletRequest request) {
		ShiroUser shiroUser = this.getShiroUser();
		List<BiSalesVolumeDay> saleList = biSaleService.selectSaleReportByGmtCreate(shiroUser.getCompanyId());
		List<BiSalesVolumeDay> saleMouthList = biSaleService.selectSaleReportByGmtCreateMouth(shiroUser.getCompanyId());
		saleList.forEach(sale -> {
			sale.setSalesVolume(NumberUtil.formatDouble2(sale.getSalesVolume()));
		});
		saleMouthList.forEach(sale -> {
			sale.setSalesVolume(NumberUtil.formatDouble2(sale.getSalesVolume()));
		});
		request.setAttribute("saleList", saleList);
		request.setAttribute("saleMouthList", saleMouthList);
		return "bi/saleReport";
	}
	
	@RequestMapping("/selectSaleReportByCategoryName")
	public String selectSaleReportByCategoryName(HttpServletRequest request) {
		List<BiSale> saleList = biSaleService.selectSaleReportByCategoryName();
		request.setAttribute("saleList", saleList);
		return "bi/saleReportByCategoryName";
	}
	
	@RequestMapping("/selectSaleReportByBand")
	public String selectSaleReportByBand(HttpServletRequest request) {
		List<BiSale> saleList = biSaleService.selectSaleReportByBand();
		request.setAttribute("saleList", saleList);
		return "bi/saleReportByBand";
	}
	
	@RequestMapping("/selectRefundReport")
	public String selectRefundReport(HttpServletRequest request) {
		ShiroUser one = this.getShiroUser();
		List<BiReturnReason> refundList = biSaleService.selectRefundReport(one.getCompanyId());
		request.setAttribute("refundList", refundList);
		return "bi/refundReport";
	}
	
	@RequestMapping("/selectSaleReportByScale")
	public String selectSaleReportByScale(HttpServletRequest request) {
		ShiroUser one = this.getShiroUser();
		List<BiSaleSizeQuantity> saleList = biSaleService.selectSaleReportByScale(one.getCompanyId());
		request.setAttribute("saleList", saleList);
		return "bi/saleReportByScale";
	}

	@RequestMapping("/selectSaleItemReportByHistory")
	public String selectSaleItemReportByHistory(HttpServletRequest request) {
		ShiroUser one = this.getShiroUser();
		Map<String, BiSale> shipMap = new HashMap<String,BiSale>();
		BiSale saleProbability = biSaleService.selectSaleItemReportByProbability(one.getCompanyId());
		List<BiSale> saleList = biSaleService.selectSaleItemReportByHistory(one.getCompanyId());
		saleList.forEach(sale -> {
			if(StringUtils.isNotBlank(sale.getSkuPic())) {
				PicModel pm = HaiJsonUtils.toBean(sale.getSkuPic(), PicModel.class);
				String imgSrc = pm.getPicList().get(0).getUrl();
				sale.setSkuPic(imgSrc);
			}
			if(shipMap.containsKey(sale.getDimensionDateStr())) {
				BiSale biSaleNew = shipMap.get(sale.getDimensionDateStr());
				biSaleNew.setCount(biSaleNew.getCount()+1);
			} else {
				BiSale biSaleNew = new BiSale();
				biSaleNew.setCount(1L);
				shipMap.put(sale.getDimensionDateStr(), biSaleNew);
			}
		});
		request.setAttribute("saleProbability", saleProbability);
		request.setAttribute("shipMap", shipMap);
		request.setAttribute("saleList", saleList);
		return "bi/saleItemReportByHistory";
	}

	@RequestMapping("/selectSaleItemReportByMouth")
	public String selectSaleItemReportByMouth(HttpServletRequest request) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		ShiroUser one = this.getShiroUser();
		BiSaleSkuMonth biSaleTemp = new BiSaleSkuMonth();
		biSaleTemp.setCompanyId(one.getCompanyId());
		biSaleTemp.setDimensionDate(sdf.format(new Date()));
		List<BiSaleSkuMonth> saleList = biSaleService.selectSaleItemReportByMouth(biSaleTemp);
		String dimensionDateStr = null;
		for (BiSaleSkuMonth sale : saleList) {
			if(StringUtils.isNotBlank(sale.getSkuPic())) {
				PicModel pm = HaiJsonUtils.toBean(sale.getSkuPic(), PicModel.class);
				String imgSrc = pm.getPicList().get(0).getUrl();
				sale.setSkuPic(imgSrc);
			}
			dimensionDateStr = sale.getDimensionDate();
		}
		if(StringUtils.isNotBlank(request.getParameter("startTime"))) {
			EntityWrapper<BiSaleSkuMonth> entityWrapper = new EntityWrapper<BiSaleSkuMonth>();
			entityWrapper.where("dimension_date={0}", request.getParameter("startTime"));
			entityWrapper.orderBy("monthly_sales", false);
			List<BiSaleSkuMonth> saleListOne = biSaleSkuMonthMapper.selectList(entityWrapper);
			String dimensionDateStrOne = null;
			for (BiSaleSkuMonth sale : saleListOne) {
				if(StringUtils.isNotBlank(sale.getSkuPic())) {
					PicModel pm = HaiJsonUtils.toBean(sale.getSkuPic(), PicModel.class);
					String imgSrc = pm.getPicList().get(0).getUrl();
					sale.setSkuPic(imgSrc);
				}
				dimensionDateStrOne = sale.getDimensionDate();
			}
			request.setAttribute("dimensionDateStrOne", dimensionDateStrOne);
			request.setAttribute("saleListOne", saleListOne);
		} else {
			BiSaleSkuMonth biSaleTempOne = new BiSaleSkuMonth();
			biSaleTempOne.setCompanyId(one.getCompanyId());
			biSaleTempOne.setDimensionDate(sdf.format(new Date()));
			List<BiSaleSkuMonth> saleListOne = biSaleService.selectSaleItemReportByMouth(biSaleTempOne);
			String dimensionDateStrOne = null;
			for (BiSaleSkuMonth sale : saleListOne) {
				if(StringUtils.isNotBlank(sale.getSkuPic())) {
					PicModel pm = HaiJsonUtils.toBean(sale.getSkuPic(), PicModel.class);
					String imgSrc = pm.getPicList().get(0).getUrl();
					sale.setSkuPic(imgSrc);
				}
				dimensionDateStrOne = sale.getDimensionDate();
			}
			request.setAttribute("dimensionDateStrOne", dimensionDateStrOne);
			request.setAttribute("saleListOne", saleListOne);
		}
		request.setAttribute("dimensionDateStr", dimensionDateStr);
		request.setAttribute("saleList", saleList);
		return "bi/saleItemReportByMouth";
	}

	@RequestMapping("/countSteph")
	public String countSteph(HttpServletRequest request) {
		List<BiPurchaseStorageLa> biSaleList = biSaleService.countStephPuchaseQuantity();
		request.setAttribute("shipList", biSaleList);
		return "bi/laPurchaseCountReport";
	}

	@RequestMapping("/allPurchaserQuantity")
	public String allPurchaserQuantity(HttpServletRequest request) {
		List<BiSale> biSaleList = biSaleService.selectAllPurchaserQuantity();
		Map<String, BiSale> shipMap = new HashMap<String,BiSale>();
		biSaleList.forEach(biSale -> {
			if(shipMap.containsKey(biSale.getPuchahseDate())) {
				BiSale biSaleNew = shipMap.get(biSale.getPuchahseDate());
				biSaleNew.setCount(biSaleNew.getCount()+1);
			} else {
				BiSale biSaleNew = new BiSale();
				biSaleNew.setCount(1L);
				shipMap.put(biSale.getPuchahseDate(), biSaleNew);
			}
		});
		request.setAttribute("shipMap", shipMap);
		request.setAttribute("shipList", biSaleList);
		return "bi/allPurchaserReport";
	}
	
	@RequestMapping("/purchasePerformance")
	public String purchasePerformance(HttpServletRequest request) {
		List<BiSale> biSaleList = biSaleService.purchasePerformance();
		Map<String, BiSale> shipMap = new HashMap<String,BiSale>();
		biSaleList.forEach(biSale -> {
			if(shipMap.containsKey(biSale.getPuchahseDate())) {
				BiSale biSaleNew = shipMap.get(biSale.getPuchahseDate());
				biSaleNew.setCount(biSaleNew.getCount()+1);
			} else {
				BiSale biSaleNew = new BiSale();
				biSaleNew.setCount(1L);
				shipMap.put(biSale.getPuchahseDate(), biSaleNew);
			}
		});
		request.setAttribute("shipMap", shipMap);
		request.setAttribute("shipList", biSaleList);
		return "bi/allPurchaserPerformance";
	}
	
	
}
